package cn.fsd.controller;

import cn.fsd.domain.Task;
import cn.fsd.query.dispatch.TaskQuery;
import cn.fsd.service.TaskService;
import cn.fsd.vo.PageBean;
import cn.fsd.vo.PointBean;
import cn.fsd.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/task",produces = "application/json;charset=utf-8")
@Api(value = "订单调度的API",description = "订单调度相关的功能")
public class TaskController {

    @Autowired
    TaskService taskService;

//    @PostMapping("/{order_id}") //post + /task/order_id
//    @ApiOperation(value = "根据订单生成任务单，*****没用了，直接OrderController的一键调度即可")
//    public ResultBean add(@PathVariable("order_id") String order_id){
//        String msg = taskService.add(order_id);
//        if("success".equals(msg)) return ResultBean.success();
//        return ResultBean.error();
//    }

    @PostMapping("/page") //post + /task/page
    @ApiOperation(value = "任务单管理页面的分页查询")
    public ResultBean findByPage(@RequestBody TaskQuery taskQuery){
        PageBean<Task> pageBean = taskService.findByPage(taskQuery);
        return ResultBean.success(pageBean);
    }

//    @GetMapping("/{task_id}") //get + /task/task_id
//    @ApiOperation(value = "根据任务单id查询收货人和分站的信息，用于查看详情页面在地图中显示标注，*****没用了，直接调用手动调度的接口即可")
//    public ResultBean showPoint(@PathVariable("task_id") String task_id){
//        List<PointBean> pointList = taskService.showPoint(task_id);
//        if(pointList.size()==2) return ResultBean.success(pointList);
//        return ResultBean.error();
//    }

    @GetMapping("/all/{task_id}") //get + /task/all/task_id
    @ApiOperation(value = "根据任务单id查询收货人和所有分站的信息，用于手动调度页面在地图中显示标注")
    public ResultBean showAllPoint(@PathVariable("task_id") String task_id){
        List<PointBean> pointList = taskService.showAllPoint(task_id);
        if(pointList.size()>=2) return ResultBean.success(pointList);
        return ResultBean.error();
    }

    @PutMapping("/{task_id}/{station_id}") //put + /task/task_id/station_id
    @ApiOperation(value = "任务单手动调度页面的修改分站")
    public ResultBean update(@PathVariable("task_id") String task_id,@PathVariable("station_id") String station_id){
        String msg = taskService.update(task_id,station_id);
        if("success".equals(msg)) return ResultBean.success();
        return ResultBean.error();
    }

    @PutMapping("/{task_id}") //put + /task/task_id
    @ApiOperation(value = "任务单调度完成进行确认，退货订单的状态修改为'已调度'")
    public ResultBean sure(@PathVariable("task_id") String task_id){
        String msg = taskService.sure(task_id);
        if("success".equals(msg)) return ResultBean.success();
        return ResultBean.error();
    }


    @GetMapping("/{order_id}")
    @ApiOperation(value = "(orderService调用)根据订单id得到任务单id")
    public String getTaskIdByOrderId(@PathVariable("order_id") String order_id){
        return taskService.getTaskIdByOrderId(order_id);
    }
}
